﻿// ———————————————————————–
// <copyright file="KMLStyle.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// KMLStyle.cs - "Contains a series of methods for parsing and formatting KML docuements."
// Project: "ICNETSOA"- "ICNETDAL"
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using ICNETServices;

namespace GISUtil.KML
{
  /// <summary>
  /// Contains methods to aid in the serializing and deserializing XML found in the Style section of KML
  /// </summary>
  [XmlRoot("Style")]
  public class KMLStyle
  {
    /// <summary>
    /// Style ID
    /// </summary>
    [XmlAttribute("Id")]
    public string ID { get; set; }

    /// <summary>
    /// The display name formatted with spaces and punctuation. 
    /// </summary>
    [XmlAttribute("friendlyname")]
    public string FriendlyName { get; set; }

    /// <summary>
    /// The IconStyle associated with this style
    /// </summary>
    [XmlElement("IconStyle")]
    public IconStyle IconStyle { get; set; }

    /// <summary>
    /// The LabelStyle associated with this style
    /// </summary>
    [XmlElement("LabelStyle")]
    private LabelStyle LabelStyle { get; set; }

    /// <summary>
    /// The LineStyle associated with this style
    /// </summary>
    [XmlElement("LineStyle")]
    private LineStyle LineStyle { get; set; }

    /// <summary>
    /// The PolyStyle associated with this style
    /// </summary>
    [XmlElement("PolyStyle")]
    private PolyStyle PolyStyle { get; set; }

    /// <summary>
    /// The BalloonStyle associated with this style
    /// </summary>
    [XmlElement("BalloonStyle")]
    private BalloonStyle BalloonStyle { get; set; }

    /// <summary>
    /// The ListStyle associated with this style
    /// </summary>
    [XmlElement("ListStyle")]
    private ListStyle ListStyle { get; set; }

    /// <summary>
    /// Determines if this class has any styling information associated with it. 
    /// </summary>
    /// <returns>True if at least one of the fields that contains style information has a value, otherwise false.</returns>
    public bool HasAValue()
    {
      return ID != null && //TODO: Is ID really required here?
              (IconStyle != null && IconStyle.HasAValue()) ||
              (LabelStyle != null && LabelStyle.HasAValue()) ||
              (LineStyle != null && LineStyle.HasAValue()) ||
              (PolyStyle != null && PolyStyle.HasAValue()) ||
              (BalloonStyle != null && BalloonStyle.HasAValue()) ||
              (ListStyle != null && ListStyle.HasAValue());
    }

    //TODO: Where does this belong?
    /// <summary>
    /// Moves an XML reader to the nearest content node that contains Text.
    /// </summary>
    /// <param name="theReader">The object that will read the xml</param>
    /// <returns>Returns null.</returns>
    public static string GetElementStringValue(XmlReader theReader)
    {
      theReader.Read(); // initialize to source node

      if (theReader.Read())
      {
        if (theReader.MoveToContent() == XmlNodeType.Text)
        {
          return theReader.Value;
        }
        else
        {
          return null;
        }
      }
      else
      {
        return null;
      }
    }

  }
}